<!DOCTYPE html>
<html lang="cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- <link href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css" rel="stylesheet">
    <script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script> -->
    <link rel="stylesheet" href="./assets/material.css">
    <script src="./assets/material.js"></script>
</head>

<body>
    <section id="demos" class="invisible">
    </section>
    <div id="app">
        <div class="video">
            <div class="warp">
                <div id="liveView" class="videoView">
                    <button id="webcamButton" class="mdc-button mdc-button--raised" style="width: 150px;display: none;">
                        <span class="mdc-button__ripple"></span>
                        <span class="mdc-button__label">启用摄像头</span>
                    </button>
                    <div style="position: relative; width: 100%;">
                        <video id="webcam" autoplay playsinline></video>
                        <canvas class="output_canvas" id="output_canvas" width="0" height="0"
                            style="position: absolute; left: 0px; top: 0px;"></canvas>
                        <p id='gesture_output' class="output">
                    </div>
                </div>

            </div>
        </div>
        <div class="game">
            <div class="container">
                <button id="start">开始游戏</button>
                <button id="pause">暂停游戏</button>
                <button id="reset">重新开始</button>
            </div>
            <canvas id="canvas"></canvas>
            <div class="information">
                <div id="score">得分: 0</div>
                <div>
                    <!-- <p>up: rotate</p>
                    <p>left: move left</p>
                    <p>right: move right</p>
                    <p>down: speed up</p> -->
                    <img src="./img.png" alt="" style="display: block;" width="90%">
                </div>
            </div>
        </div>
    </div>


    <script type="module">
        import {
            GestureRecognizer,
            FilesetResolver,
            DrawingUtils
        } from "./assets/tasks-vision@0.10.3.js"
        import { left, right, rotation } from './assets/game.js';

        const demosSection = document.getElementById("demos");
        let gestureRecognizer;
        let runningMode = "VIDEO";
        let enableWebcamButton;
        let webcamRunning = false;

        const videoHeight = "360px";
        const videoWidth = "480px";

        // Before we can use HandLandmarker class we must wait for it to finish
        // loading. Machine Learning models can be large and take a moment to
        // get everything needed to run.
        const createGestureRecognizer = async () => {
            const vision = await FilesetResolver.forVisionTasks(
                "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.3/wasm"
            );
            gestureRecognizer = await GestureRecognizer.createFromOptions(vision, {
                baseOptions: {
                    modelAssetPath: "./gesture_recognizer.task",
                    delegate: "CPU"
                },
                runningMode: runningMode
            });
            demosSection.classList.remove("invisible");
        };
        await createGestureRecognizer();


        /********************************************************************
        // Demo 2: Continuously grab image from webcam stream and detect it.
        ********************************************************************/

        const video = document.getElementById("webcam");
        const canvasElement = document.getElementById("output_canvas");
        const canvasCtx = canvasElement.getContext("2d");
        const gestureOutput = document.getElementById("gesture_output");

        // Check if webcam access is supported.
        function hasGetUserMedia() {
            return !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia);
        }

        // If webcam supported, add event listener to button for when user
        // wants to activate it.
        if (hasGetUserMedia()) {
            enableWebcamButton = document.getElementById("webcamButton");
            enableWebcamButton.addEventListener("click", enableCam);
        } else {
            console.warn("getUserMedia() is not supported by your browser");
        }

        // Enable the live webcam view and start detection.
        function enableCam(event) {
            if (!gestureRecognizer) {
                alert("Please wait for gestureRecognizer to load");
                return;
            }

            if (webcamRunning === true) {
                webcamRunning = false;
                enableWebcamButton.innerText = "启用预测";
            } else {
                webcamRunning = true;
                enableWebcamButton.innerText = "禁用预测";
            }

            // getUsermedia parameters.
            const constraints = {
                video: true
            };

            // Activate the webcam stream.
            navigator.mediaDevices.getUserMedia(constraints).then(function (stream) {
                video.srcObject = stream;
                console.log(video);
                //video.play();
                // predictWebcam();
                video.addEventListener("loadedmetadata", () => {
                    console.log("loadedata");
                    //video.play();
                    predictWebcam();
                });
            });
        }
        enableCam();

        let lastVideoTime = -1;
        let results = undefined;

        let t = 0;

        const controller = (categoryName) => {
            t++;
            if (t > 20) {
                if (categoryName === "right") {
                    gestureOutput.innerText = "右"
                    right();
                }
                else if (categoryName === "left") {
                    gestureOutput.innerText = "左"
                    left();
                }
                else if (categoryName === "rotation") {
                    gestureOutput.innerText = "旋转"
                    rotation();
                }
                else {
                    gestureOutput.innerText = "";
                }
                t = 0;
            }

        }

        function predictWebcam() {
            const webcamElement = document.getElementById("webcam");
            // Now let's start detecting the stream.
            // if (runningMode === "IMAGE") {
            //     runningMode = "VIDEO";
            //     await gestureRecognizer.setOptions({ runningMode: "VIDEO" });
            // }
            let nowInMs = Date.now();
            if (video.currentTime !== lastVideoTime) {
                lastVideoTime = video.currentTime;
                results = gestureRecognizer.recognizeForVideo(video, nowInMs);
            }

            canvasCtx.save();
            canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
            const drawingUtils = new DrawingUtils(canvasCtx);

            canvasElement.height = 720;
            canvasElement.style.height = videoHeight;
            webcamElement.style.height = videoHeight;

            canvasElement.width = 1280;
            canvasElement.style.width = videoWidth;
            webcamElement.style.width = videoWidth;
            // console.log(canvasElement);




            if (results.landmarks) {
                for (const landmarks of results.landmarks) {
                    drawingUtils.drawConnectors(
                        landmarks,
                        GestureRecognizer.HAND_CONNECTIONS,
                        {
                            color: "#00FF00",
                            lineWidth: 5
                        }
                    );
                    drawingUtils.drawLandmarks(landmarks, {
                        color: "#FF0000",
                        lineWidth: 2
                    });
                }
            }

            canvasCtx.restore();
            if (results.gestures.length > 0) {
                gestureOutput.style.display = "block";
                gestureOutput.style.width = videoWidth;
                const categoryName = results.gestures[0][0].categoryName;
                const categoryScore = parseFloat(
                    results.gestures[0][0].score * 100
                ).toFixed(2);
                const handedness = results.handednesses[0][0].displayName;
                //gestureOutput.innerText = `GestureRecognizer: ${categoryName}\n Confidence: ${categoryScore} %\n Handedness: ${handedness}`;
                // =============
                controller(categoryName);



            } else {
                gestureOutput.style.display = "none";
            }
            // Call this function again to keep predicting when the browser is ready.
            // if (webcamRunning === true) {
            window.requestAnimationFrame(predictWebcam);
        }
        console.log(window);
        // while (1) {
        //     predictWebcam();
        // }

    </script>
</body>

</html>

<!-- Copyright 2023 The MediaPipe Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. -->

<!-- <h1>Recognize hand gestures using the MediaPipe HandGestureRecognizer task</h1> -->


<style>
    /* Copyright 2023 The MediaPipe Authors.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    
         http://www.apache.org/licenses/LICENSE-2.0
    
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License. */

    @use "@material";

    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }

    body {
        font-family: roboto;
        color: #3d3d3d;
        --mdc-theme-primary: #007f8b;
        --mdc-theme-on-primary: #f1f3f4;
        overflow: hidden;
    }

    h1 {
        color: #007f8b;
    }

    h2 {
        clear: both;
    }

    video {
        clear: both;
        display: block;
        transform: rotateY(180deg);
        -webkit-transform: rotateY(180deg);
        -moz-transform: rotateY(180deg);
        height: 280px;
    }

    section {
        opacity: 1;
        transition: opacity 500ms ease-in-out;
    }

    .removed {
        display: none;
    }

    .invisible {
        opacity: 0.2;
    }

    .detectOnClick {
        position: relative;
        float: left;
        width: 48%;
        margin: 2% 1%;
        cursor: pointer;
    }

    .videoView {
        /* position: absolute;
        float: left;
        width: 48%;
        margin: 2% 1%; */
        width: 100%;
        cursor: pointer;
        min-height: 500px;
    }

    .videoView p,
    .detectOnClick p {
        padding-top: 5px;
        padding-bottom: 5px;
        background-color: #007f8b;
        color: #fff;
        border: 1px dashed rgba(255, 255, 255, 0.7);
        z-index: 2;
        margin: 0;

    }

    .highlighter {
        background: rgba(0, 255, 0, 0.25);
        border: 1px dashed #fff;
        z-index: 1;
        position: absolute;
    }

    .canvas {
        z-index: 1;
        position: absolute;
        pointer-events: none;
    }

    .output_canvas {
        transform: rotateY(180deg);
        -webkit-transform: rotateY(180deg);
        -moz-transform: rotateY(180deg);
    }

    .detectOnClick {
        z-index: 0;
        font-size: calc(8px + 1.2vw);
    }

    .detectOnClick img {
        width: 45vw;
    }

    .output {
        display: none;
        width: 100%;
        font-size: calc(8px + 1.2vw);
        text-align: center;
    }
</style>

<style>
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }

    #app {
        width: 100vw;
        height: 100vh;
        display: flex;
    }

    #app .video {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    #app .video .warp {
        height: 300px;
    }

    #app .video .warp .result {
        text-align: center;
    }

    #app .game {
        flex: 2;
    }
</style>

<style>
    .game {
        /* margin: 0;
  width: 100%;
  height: 100vh; */
        display: flex;
        align-items: center;
        justify-content: center;

        background-color: rgb(54, 44, 44);
        background-image: url(./bg.png);
        background-position: center;
    }

    #canvas {
        border: 2px solid rgb(109, 109, 109);
    }

    .container {
        display: flex;
        flex-direction: column;
        margin-right: 5rem;
    }

    button {
        width: 90px;
        height: 45px;
        margin-bottom: 1rem;
        border-radius: 1000px;
    }

    button {
        outline: none;
    }

    #score {
        font-size: 2rem;
    }

    .information {
        color: #fff;
        margin-left: 5rem;
    }
</style>